import urllib.request
import re


def find_chapter_urls(index_url):
    response = urllib.request.urlopen(index_url)
    html = response.read().decode('utf-8')
    title_regex = r'<title>(?P<comic_name>.+)</title>'
    title = re.search(title_regex, html)
    chapter_url_regex = r'<a href=\"(?P<chapter>' + re.escape(index_url) + r'[^\"]+)\".+><span>(?P<chapter_name>.+)</span></a>'
    chapter_urls = re.findall(chapter_url_regex, html)
    return title.group(1), chapter_urls


def get_img_urls_from_page(page_url):
    response = urllib.request.urlopen(page_url)
    html = response.read().decode('utf-8')
    image_regex = r'<div\sclass=\"image-content\">\s*<img\ssrc=\"(?P<img>[^\"]+)\".+/>\s*</div>'
    image_urls = re.findall(image_regex, html)
    return image_urls



